{extend name="../template/admin/head.html"}
{block name="title"}
导航设置_{__block__}
{/block}
{block name="content"}
<blockquote class="layui-elem-quote news_search">
<a class="layui-btn linksAdd_btn" style="background-color:#5FB878" id="tjnav">添加导航</a>  说明：系统默认的导航无法删除或编辑
  </blockquote>
  <div class="layui-form news_list">
      <table class="layui-table">
        <colgroup>
        <col width="9%">
        <col width="9%">
        <col width="9%">
        <col width="9%">
        <col width="9%">
        <col width="9%">
        <col width="9%">
        <col width="15%">
        </colgroup>
        <thead>
        <tr>
          <th>序号</th>
          <th style="text-align:left;">名称</th>
          <th>Url</th>
          <th>类型</th>
          <th>操作</th>
        </tr> 
        </thead>
        <tbody class="news_content">
  <colgroup>
    <col width="150">
    <col width="200">
    <col>
  </colgroup>
  <tbody>
{volist name="res" id="res"}
    <tr id="data{$res.id}">
      <th>
        <input type="text" id="nav{$res.id}" placeholder="顺序" class="layui-input" value="{$res.num}"></th>
        <td id="navname{$res.id}">{$res.navname}</td>
        <td id="navurl{$res.id}">{$res.url}</td>
        <td>{if condition="($res.issystem == 'y')"}系统{else/}自定义{/if}</td>
        <td>
      <div class="layui-btn-container">
        <button class="layui-btn {if condition="($res.issystem == 'y')"}layui-btn-disabled"{else/}"onclick="editnav({$res.id})"{/if} >编辑</button>
        <button class="layui-btn {if condition="($res.issystem == 'y')"}layui-btn-disabled"{else/}"onclick="delnav({$res.id})"{/if}>删除</button>
      </div>
      </td>
    </tr>
{/volist}
  </tbody>
</table>

        </tbody>
  </div>
  </div>

{/block}
{block name="jssrc"}
<script type="text/javascript">
layui.use(['form', 'layedit', 'laydate'], function() {
var form = layui.form,
layer = layui.layer,
layedit = layui.layedit,
laydate = layui.laydate;
});
//添加导航
$('#tjnav').on('click', function(){
    layer.open({
      title:'添加导航',
      type: 1,
      area: ['400px', '300px'],
      shadeClose: true, //点击遮罩关闭
      content: ' \<\div style="margin-top:10px; padding-right:10px;">\<\label class="layui-form-label">序号：\<\/label>\<\div class="layui-input-block">\<\input type="text" class="layui-input newsName" lay-verify="required" placeholder="序号" id="num"><br>\<\/div>\<\label class="layui-form-label">名称：\<\/label>\<\div class="layui-input-block">\<\input type="text" class="layui-input newsName" lay-verify="required" placeholder="导航名称" id="name"><br>\<\/div>\<\label class="layui-form-label">地址：\<\/label>\<\div class="layui-input-block">\<\input type="text" class="layui-input newsName" lay-verify="required" placeholder="地址" id="url">\<\/div>\<\/div><button class="layui-btn kit-login-btn" lay-submit="submit" lay-filter="tjnav" id="submit" style=" float: right;margin-right:10px;margin-top:5px;">添加</button>'

    });
  }); 
//编辑框显示并传递值
function editnav(num){
    var id = num;
    var odlnum = $('#nav'+num).val();
    var odlname = $('#navname'+num).html();
    var odlurl = $('#navurl'+num).html();
    var content= "<div style=\"margin-top:10px; padding-right:10px;\">";
    content+= "<label class=\"layui-form-label\">序号：</label>";
    content+= "<div class=\"layui-input-block\">";
    content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"序号\" id=\"newnum\" value="+odlnum+"></div><br>";
    content+= "<label class=\"layui-form-label\">名称：</label>";
    content+= "<div class=\"layui-input-block\">";
    content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"导航名称\" id=\"newname\" value="+odlname+"></div><br>";
    content+= "<label class=\"layui-form-label\">地址：</label><div class=\"layui-input-block\"><input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"地址\" id=\"newurl\" value="+odlurl+"></div><br>";
    content+= "</div>";
    content+= "<button class=\"layui-btn kit-login-btn\" lay-submit=\"submit\" lay-filter=\"wrnav\" id=\"wrnav\" style=\" float: right;margin-right:10px;margin-top:5px;\">修改</button>";
    layer.open({
      title:'编辑导航',
      type: 1,
      area: ['400px', '300px'],
      shadeClose: true, //点击遮罩关闭
      content: content,

    });
    //编辑
layui.use('form', function(){
  var form = layui.form;    
 form.on('submit(wrnav)', function(date) {
  var newnum = $("#newnum").val();
  var newname = $("#newname").val();
  var newurl = $("#newurl").val();
  if (newnum != "" && newname != "" && newurl != "") {
        $.ajax({
          type:"POST",
          url:"{:URL('Systemset/navpostedit')}",
          dataType:"json",
          data:{id:id,newnum:newnum,newname:newname,newurl:newurl},
          beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#wrnav").attr({"disabled":"disabled"});
            $("#wrnav").addClass("layui-btn-disabled");
            $("#wrnav").html("修改中...");
            layer.load(1);
            },
           error: function (XmlHttpRequest, textStatus, errorThrown) {
              if (XmlHttpRequest.responseText != "") {
                  layer.msg('提交失败，请稍后尝试！');
                  layer.closeAll('loading');
                  $("#wrnav").removeClass("layui-btn-disabled");
                  $("#wrnav").removeAttr("disabled");  
                  $("#wrnav").html("修改");
              }
          },
          success:function(data){
            //1为修改成功
            //2为修改失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("修改");
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("修改");
            }
          }
        });
        return false;
    }else{
      layer.msg('所有内容不能为空!');
      return false;
    }
  });
});

}
//删除导航数据
function delnav(id){
layer.confirm('确认删除？', {
  btn: ['确认','取消'] //按钮
}, function(){
  if (id != "") {
        $.ajax({
          type:"POST",
          url:"{:URL('Systemset/navpostdel')}",
          dataType:"json",
          data:{id:id},
          beforeSend: function () {
            // 动画显示
            layer.load(1);
            },
           error: function (XmlHttpRequest, textStatus, errorThrown) {
              if (XmlHttpRequest.responseText != "") {
                  layer.msg('提交失败，请稍后尝试！');
              }
          },
          success:function(data){
            //1为删除成功
            //2为删除失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $('#data'+id).remove();
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
            }
          }
        });
        return false;
    }else{
      layer.msg('ID值为空!');
      return false;
    }
});
}
//添加&编辑导航数据
layui.use('form', function(){
  var form = layui.form;         
  //添加
 form.on('submit(tjnav)', function(data) {
  var num = $("#num").val();
  var name = $("#name").val();
  var url = $("#url").val();
  if (num != "" && name != "" && url != "") {
        $.ajax({
          type:"POST",
          url:"{:URL('Systemset/navpostadd')}",
          dataType:"json",
          data:{num:num,name:name,url:url},
          beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#tjnav").attr({"disabled":"disabled"});
            $("#tjnav").addClass("layui-btn-disabled");
            $("#tjnav").html("添加中...");
            layer.load(1);
            },
           error: function (XmlHttpRequest, textStatus, errorThrown) {
              if (XmlHttpRequest.responseText != "") {
                  layer.msg('提交失败，请稍后尝试！');
                  $("#tjnav").removeClass("layui-btn-disabled");
                  $("#tjnav").removeAttr("disabled");  
                  $("#tjnav").html("添加");
              }
          },
          success:function(data){
            //1为添加成功
            //2为添加失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#tjnav").removeClass("layui-btn-disabled");
              $("#tjnav").removeAttr("disabled");  
              $("#tjnav").html("添加");
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#tjnav").removeClass("layui-btn-disabled");
              $("#tjnav").removeAttr("disabled");  
              $("#tjnav").html("添加");

            }
          }
        });
        return false;
    }else{
      layer.msg('所有内容不能为空!');
      return false;
    }
});

});    
</script>
{/block}